export function getValue(obj, name) {
  // 获取形如key.a.b.c的值的方法
  if (!obj) {
    return obj;
  }
  let nameList = name.split(".");
  let len = nameList.length;
  let temp = obj;
  // 层层递进的方式获取
  for (let i = 0; i < len; i++) {
    if (temp[nameList[i]]) {
      temp = temp[nameList[i]];
    } else {
      return undefined;
    }
  }
  return temp;
}
export function setValue(obj, attr, value) {
  // obj对象，attr属性，value值
  // 处理形如_data = {obj:{x:1, y:2}}的方法
  // 传入(_data, obj.x, 2)
  if (!obj) {
    return;
  }
  let attrList = attr.split(".");
  let len = attrList.length - 1; // 获取最后一层的上一层的属性
  let temp = obj;
  for (let i = 0; i < len; i++) {
    if (temp[attrList[i]]) {
      temp = temp[attrList[i]];
    } else {
      return false;
    }
  }
  if (temp[attrList[len]] != null) {
    temp[attrList[len]] = value;
  }
}
export function mergeAttr(obj1, obj2) {
  // 属性合并
  if (!Boolean(obj1)) {
    return clone(obj2);
  }
  if (!Boolean(obj2)) {
    return clone(obj1);
  }
  let result = {};
  let obj1Attrs = Object.getOwnPropertyNames(obj1);
  for (let i = 0; i < obj1Attrs.length; i++) {
    result[obj1Attrs[i]] = obj1[obj1Attrs[i]];
  }
  let obj2Attrs = Object.getOwnPropertyNames(obj2);
  for (let i = 0; i < obj2Attrs.length; i++) {
    result[obj2Attrs[i]] = obj2[obj2Attrs[i]];
  }
  return result;
}
function easyClone(obj) {
  // 无法合并代理对象，比如vm._data
  JSON.parse(JSON.stringify(obj));
}
function clone(obj) {
  if (obj instanceof Array) {
    return cloneArray(obj);
  } else if (obj instanceof Object) {
    return cloneObject(obj);
  } else {
    return obj;
  }
}
function cloneObject(obj) {
  // 克隆对象
  let result = {};
  let names = Object.getOwnPropertyNames(obj); // 这样可以获取所有代理对象的属性
  for (let i = 0; i < names.length; i++) {
    result[names[i]] = clone(obj[names[i]]);
  }
  return result;
}
function cloneArray(obj) {
  // 克隆数组
  let result = new Array(obj.length);
  for (let i = 0; i < obj.length; i++) {
    result[i] = clone(obj[i]);
  }
  return result;
}
export function getEnvAttr(vm, vnode) {
  // 获取当前环境变量
  // debugger;
  let result = mergeAttr(vm._data, vnode.env);
  result = mergeAttr(result, vm.computed);
  return result;
}
